package com.example.parkingproject.service.serviceImp;

import com.example.parkingproject.code.ServiceCode;
import com.example.parkingproject.ex.ServiceException;
import com.example.parkingproject.jopo.dto.AddCarDto;
import com.example.parkingproject.jopo.entity.AddCar;
import com.example.parkingproject.jopo.entity.car;
import com.example.parkingproject.jopo.entity.carIn;
import com.example.parkingproject.jopo.vo.*;
import com.example.parkingproject.mapper.CarMapper;
import com.example.parkingproject.service.CarService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.context.Theme;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class CarServiceImp implements CarService {
    @Autowired
    CarMapper carMapper;

    @Override
    public void carIn(carIn carIn) {
        log.debug("传入参数车辆{}",carIn);
         Integer blickList = carMapper.getBlickList(carIn.getCarId());
         log.debug("查询出的数据{}",blickList);
        if (blickList>0){
            throw new ServiceException(ServiceCode.error_bad_request,"该车辆处于给名单请与物业联系");
        }
        CarInVo historyCar = carMapper.getHistoryCar(carIn.getCarId());
        if (historyCar != null) {
            throw new ServiceException(ServiceCode.error_bad_request, "该车牌号车库已存在");
        }
        List<Integer> location = carMapper.getLocation();
        log.debug("查询出的车位参数{}", location);
        if (location.size() == 0) {
            throw new ServiceException(ServiceCode.error_bad_request, "车位已经停满了");
        }
        Integer row = carMapper.getMonthly(carIn.getCarId());
        log.debug("获取是否为月租用户行数{}", row);
        CarInVo carInVo = carMapper.getCarInVo(carIn.getCarId());
        car car = new car();
        String cardId = carMapper.getCardId(carIn.getCarId());
        if (row != 0) {
            log.debug("查询出的身份证{}", cardId);
            carMapper.update(cardId);
            Integer locationNumber = carMapper.getLocationNumber(cardId);
            car.setLocation(locationNumber);
        } else if (carIn.getLocationNumber() != null) {
            car.setLocation(carIn.getLocationNumber());
        } else {
            Random random = new Random();
            Integer space = location.get(random.nextInt(location.size()));
            log.debug("分配到的车位号{}", space);
            carMapper.reloadspace(space);
            car.setLocation(space);
        }
        BeanUtils.copyProperties(carInVo, car);
        car.setInTime(new Date());
        log.debug("即将处理传入的停车参数{}", car);
        car.setCardId(cardId);
        log.debug("传入的车主身份证信息{}", cardId);
        carMapper.carIn(car);
    }

    @Override
    public Double carOut(String carId) {
        CarInVo historyCar = carMapper.getHistoryCar(carId);
        log.debug("查询出来的历史停车参数{}", historyCar);
        if (historyCar == null) {
            throw new ServiceException(ServiceCode.error_bad_request, "该车不存在记录");
        }
        log.debug("传入的更改车位参数{}", historyCar.getLocation());
        carMapper.reloadspaceAgain(historyCar.getLocation());
        car car = new car();
        BeanUtils.copyProperties(historyCar, car);
        car.setOutTime(new Date());
        log.debug("获取的车辆参数{}", car);
        Long inTime = car.getInTime().getTime();
        Long outTime = car.getOutTime().getTime();

        MonthVo monthUser = carMapper.getMonth(carId);
        log.debug("获取到的月租用户信息{}", monthUser);
        log.debug("获取到的停车时间{}",outTime-inTime);
        double stopTime = (outTime - inTime) / 1000 / 60 / 60;
        log.debug("停车时间为{}小时", stopTime);
        if (monthUser != null) {
            car.setStopTime(stopTime);
            car.setPrice(0D);
            carMapper.carOut(car);
            carMapper.reupdate(historyCar.getCardId());
            return 0D;
        }
        int vip = monthUser.getVip();
        if (vip == 1) {
            Double price = carMapper.getVipprice();
            log.debug("vip单价{}",price);
            log.debug("停车时长{}",stopTime);
            Double pay = price * stopTime;
            log.debug("应付款{}", pay);
            car.setPrice(pay);
            car.setStopTime(stopTime);
            carMapper.carOut(car);
            return pay;
        } else {
            Double price = carMapper.getprice();
            log.debug("普通用户单价{}",price);
            log.debug("停车时长{}",stopTime);
            Double pay = price * stopTime;
            log.debug("应付款{}", pay);
            car.setStopTime(stopTime);
            carMapper.carOut(car);
            return pay;
        }


    }

    @Override
    public void addNewCar(AddCarDto addCarDto) {
        log.debug("传入的车牌参数{}", addCarDto.getCarId());
        AddCarVo addCarVo = carMapper.getAddCarVo(addCarDto.getCardId());
        Integer car = carMapper.getCar(addCarDto.getCarId());
        if (car != 0) {
            throw new ServiceException(ServiceCode.error_bad_request, "车辆已经添加");
        }
        AddCar addCar = new AddCar();
        String carId = addCarDto.getCarId();
        BeanUtils.copyProperties(addCarVo, addCar);
        log.debug("车牌号{}", carId);
        Integer vip = carMapper.getUserVip(addCarDto.getCardId());
        log.debug("拿到的vip参数{}", vip);
        addCar.setCarId(carId);
        addCar.setVip(vip);
        log.debug("添加数据库的参数{}", addCar);
        Integer row = carMapper.getMonthly(addCarDto.getCardId());
        if (0 != row) {
            addCar.setMonthlyCar(1);
        } else {
            addCar.setMonthlyCar(0);
        }
        carMapper.addCar(addCar);
    }

    @Override
    public List<StopHistoryVo> getHistory() {
        List<StopHistoryVo> list = carMapper.getHistory();
        return list;
    }

    @Override
    public List<StopHistoryVo> search(StopHistoryVo stopHistoryVo) {
        List<StopHistoryVo> list = carMapper.search(stopHistoryVo);
        if (list.size() == 0) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "未查询到数据");
        }
        return list;
    }

    @Override
    public Integer getDayHistory() {
        Integer count = carMapper.getDayHistory();
        return count;
    }

    @Override
    public Double getMonthPrice() {
        Double price = carMapper.getMonthPrice();
        return price;
    }

    @Override
    public List<Integer> weekend() {
        List<Integer> list =carMapper.weekend();
        log.debug("传出参数{}统计",list);
        return list;
    }

    @Override
    public List<Integer> orderPrice() {
        List<Integer> list = carMapper.orderPrice();
        return list;
    }

    @Override
    public List<CarListVo> getCarList(String cardId) {
        return carMapper.getCarList(cardId);
    }


}
